#!/bin/bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"

modules=(lsp psp dlt com tsyn dm em pm phm ucm nm dlt-daemon iam sm socom cryptoapi)

function usage() {
    echo "Usage $( basename $0 ) [-h | -d] [<module> ...]"
    echo "    build eb platform module"
    echo "    <moduel>: ${modules[@]},如果不指定，编译所有module"
    echo "    adg platform module必须在当前目录下"
    echo "    参数:"
    echo "    -h: 输出本信息"
    echo "    -d: 编译依赖包"
    exit 1
}

function valid_mod() {
    local mod="$1"
    for m in "${modules[@]}"
    do
        if [ "$m" == "$mod" ]
        then
            echo 0
            return
        fi
    done
    echo 1
}

paraDep="--without-dependencies"
while getopts ":hd" args
do
    case "$args" in
    h)
        usage
        ;;
    d)
       paraDep=""
       ;;
    *)
        usage
        ;;
    esac
    shift
done

n=1
while [ $n -le $# ]
do
   m=${!n} 
   if [ 1 -eq $( valid_mod $m ) ]
   then
        echo "错误的模块名: $m"
        echo "模块列表:${modules[@]}"
        exit 2
   fi
   n=$(( $n + 1 ))
done

buildType=RelWithDebInfo

if [ 0 -eq $# ]
then
    cmd="ara-cli Build --cpus 16 --target-os EBLinux --target-platfor qemu-x86 --build-type ${buildType}"
    echo "${cmd}"
    ${cmd} 
else
    while [ 0 -lt $# ]
    do
        cmd="ara-cli Build --cpus 16 --target-os EBLinux --target-platfor qemu-x86 --build-type ${buildType} ${paraDep} --module $1"
        echo "${cmd}"
        ${cmd} 
        shift
    done
fi

        

